package xyz.hubery.disruptor;

import com.lmax.disruptor.EventHandler;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import xyz.hubery.domain.Order;
import xyz.hubery.match.MatchServiceFactory;
import xyz.hubery.match.MatchStrategy;
import xyz.hubery.match.impl.LimitMatchServiceImpl;
import xyz.hubery.model.OrderBooks;

/**
 * 该对象 有多个: 和Symbol的数据对应
 * 针对某一个OrderEventHandler , 同一时间只会有一个线程来执行它
 */
@Data
@Slf4j
public class OrderEventHandler implements EventHandler<OrderEvent> {

    private OrderBooks orderBooks;

    private String symbol;

    public OrderEventHandler(OrderBooks orderBooks) {
        this.orderBooks = orderBooks;
        this.symbol = this.orderBooks.getSymbol();
    }

    @Override
    public void onEvent(OrderEvent orderEvent, long l, boolean b) throws Exception {
        Order order = (Order) orderEvent.getSource();
        if (!order.getSymbol().equals(this.symbol)) { // 不处理非当前交易对的事件
            return;
        }
        log.info("开始接收订单事件," + orderEvent);

        // 撮合(匹配订单/吃单
        MatchServiceFactory.getInstance(MatchStrategy.LIMIT_MATCH).match(orderBooks,(Order) orderEvent.getSource());

        log.info("订单处理完毕...");
    }
}